/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.zt.dangyuan;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import org.xutils.ex.HttpException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.zt.dangyuan.customView.OnRefreshListener;
import io.zt.dangyuan.customView.RefreshListView;
import io.zt.dangyuan.object.ContentListObject;
import io.zt.dangyuan.util.GlideImageLoader;


/**
 * 党建动态
 */
public class
ActivityItem1 extends BaseActivity implements OnRefreshListener {

    private Banner banner_rebatehome_banner;//首页banner图
    private int pos;
//    SlideShowView slidShow;
    private LinearLayout linearLayout;
    List<ContentListObject.ListEntity> imagesListObejects;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addCenterView(R.layout.main_item1);
        toolbar.setNavigationIcon(R.drawable.icon_select_all_light);

        listView = (RefreshListView) findViewById(R.id.activity_item1_listview);
        banner_rebatehome_banner = (Banner) findViewById(R.id.banner_rebatehome_banner);
        bbsMainAdapter2 = new BBSItemAdapter1(this, listObejects);
        imagesListObejects = new ArrayList<ContentListObject.ListEntity>();

        linearLayout = (LinearLayout) View.inflate(this, R.layout.main_item3, null);
//        slidShow = (SlideShowView) linearLayout.findViewById(R.id.slidshow);
        listView.addHeaderView(linearLayout);
        pos = getIntent().getIntExtra("pos", 0);
//        slidShow.setOnItemclickLis(new SlideShowView.UrlItemClickLis() {
//            @Override
//            public void OnUrlItemClick(int p, View v) {
//
//                Intent intent = new Intent(ActivityItem1.this, BBSDetailActivity.class);
//                ContentListObject.ListEntity object = imagesListObejects.get(p);
//
//                intent.putExtra("contentid", object.getArcId());
//                switch (pos) {
//                    case 2:
//                        intent.putExtra("type", BBSDetailActivity.TYPE_ZHONGCHOU);
//                        break;
//                    case 4:
//                        intent.putExtra("type", BBSDetailActivity.TYPE_COMMENT);
//                        break;
//                    case 6:
//                        intent.putExtra("type", BBSDetailActivity.TYPE_COMMENT);
//                        break;
//                    case 7:
//                        intent.putExtra("type", BBSDetailActivity.TYPE_COMMENT);
//                        break;
//                }
//
//                intent.putExtra("commotcount", object.getComments());
//                startActivity(intent);
//
//            }
//        });
        switch (pos) {
            case 1:
                setTitleImg(R.drawable.title_huodong);
                break;
            case 2:
                setTitleImg(R.drawable.title_zhongchou);
                contentListRequest.setCid("zcgy");
                cid = "zcgy";
                break;

            case 6://党建动态展示幻灯片
                setTitleImg(R.drawable.title_dangjian);
                contentListRequest.setCid("djdt");
                cid = "djdt";
                break;
            case 7:
                setTitleImg(R.drawable.title_gongzuo);
                contentListRequest.setCid("gzbs");
                cid = "gzbs";
                break;
        }
        imageWebRequst();
        listView.setAdapter(bbsMainAdapter2);
        listView.setOnRefreshListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(ActivityItem1.this, BBSDetailActivity.class);
                ContentListObject.ListEntity object = listObejects.get(position-2);

                intent.putExtra("contentid", object.getArcId());
                switch (pos) {
                    case 2:
                        intent.putExtra("type", BBSDetailActivity.TYPE_ZHONGCHOU);
                        break;
                    case 4:
                        intent.putExtra("type", BBSDetailActivity.TYPE_COMMENT);
                        break;
                    case 6:
                        intent.putExtra("type", BBSDetailActivity.TYPE_COMMENT);
                        break;
                    case 7:
                        intent.putExtra("type", BBSDetailActivity.TYPE_COMMENT);
                        break;
                }


                intent.putExtra("commotcount", object.getComments());
                startActivity(intent);
            }
        });
        webRequst(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                startActivity(new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                finish();
                return true;
            case R.id.action_search: {
                Intent intent = new Intent(ActivityItem1.this, SearchActivity.class);
                intent.putExtra("cid", cid);

                switch (pos) {
                    case 2:
                        intent.putExtra("type", BBSDetailActivity.TYPE_ZHONGCHOU);
                        break;
                    case 4:
                        intent.putExtra("type", BBSDetailActivity.TYPE_COMMENT);
                        break;
                    case 6:
                        intent.putExtra("type", BBSDetailActivity.TYPE_COMMENT);
                    case 7:
                        intent.putExtra("type", BBSDetailActivity.TYPE_COMMENT);
                        break;
                }

                startActivity(intent);
                return true;
            }

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDownPullRefresh() {

        listObejects.clear();
        contentListRequest.setTapType("1");
        webRequst(false);


//        new AsyncTask<Void, Void, Void>() {
//
//            @Override
//            protected Void doInBackground(Void... params) {
//                listObejects.clear();
//                contentListRequest.setTapType("1");
//                webRequst();
//                return null;
//            }
//
//            @Override
//            protected void onPostExecute(Void result) {
//                listView.hideHeaderView();
//            }
//        }.execute(new Void[]{});
    }

    @Override
    public void onLoadingMore() {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                contentListRequest.setTapType("2");
                webRequst(false);

                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                // 控制脚布局隐藏
//                listView.hideFooterView();
            }
        }.execute(new Void[]{});
    }

    public void imageWebRequst() {//点击党员动态掉的接口

        HashMap<String,Object> map = new HashMap<String,Object>();
        map.put("arctype", "2");
        map.put("cid", cid);
        map.put("pagesize","5");
        WebUtils.sendPost(ActivityItem1.this, "contentList", map, new WebUtils.OnWebListenr() {
            @Override
            public void onSuccess(boolean isSuccess, String msg, String data) {
                if (isSuccess) {
                    imagesListObejects.clear();
                    ContentListObject resultData = JSON.parseObject(data, ContentListObject.class);
                   if(resultData!=null&&resultData.getList()!=null&&resultData.getList().size()>0){
                       initBanner(resultData);
                   }else {
                       banner_rebatehome_banner.setVisibility(View.GONE);
                   }
                }
            }

            @Override
            public void onFailed(HttpException error, String msg) {
                Toast.makeText(ActivityItem1.this,"数据加载失败"+msg,Toast.LENGTH_LONG).show();
            }

        });
    }


    //轮播图片banner图
    private void initBanner(final ContentListObject resultData) {
        List<String> bannerlist = new ArrayList<String>();
        for (int i = 0; i < resultData.getList().size(); i++) {
//            bannerlist.add(resultData.getList().get(i).getTitleImg());
            bannerlist.add( "http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=a&step_word=&hs=0&pn=11&spn=0&di=190511523290&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=825673058%2C3151377622&os=2109568335%2C2176433741&simid=3499446495%2C371433811&adpicid=0&lpn=0&ln=1592&fr=&fmq=1534492257663_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201404%2F03%2F20140403114640_zWLAY.jpeg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3B17tpwg2_z%26e3Bv54AzdH3Frj5rsjAzdH3F4ks52AzdH3Fdnacnb88dAzdH3F1jpwtsAzdH3F&gsm=0&rpstart=0&rpnum=0&islist=&querylist="
            );
           }
        banner_rebatehome_banner.setImageLoader(new GlideImageLoader());
        banner_rebatehome_banner.setImages(bannerlist);
        banner_rebatehome_banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent intent = new Intent(ActivityItem1.this, BBSDetailActivity.class);
                ContentListObject.ListEntity object = resultData.getList().get(position);

                intent.putExtra("contentid", object.getArcId());
                switch (pos) {
                    case 2:
                        intent.putExtra("type", BBSDetailActivity.TYPE_ZHONGCHOU);
                        break;
                    case 4:
                        intent.putExtra("type", BBSDetailActivity.TYPE_COMMENT);
                        break;
                    case 6:
                        intent.putExtra("type", BBSDetailActivity.TYPE_COMMENT);
                        break;
                    case 7:
                        intent.putExtra("type", BBSDetailActivity.TYPE_COMMENT);
                        break;
                }

                intent.putExtra("commotcount", object.getComments());
                startActivity(intent);
            }
        });
        banner_rebatehome_banner.start();
        bannerlist.clear();
    }

}
